home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / mac / Shout3Ddemo / S3D_2E1.exe / Shout3d_runtime / codebase / custom_nodes / LabelEffect.java < prev    next >
Text File  |  2000-10-27  |  5KB  |  131 lines

  1. /**    
  2.     Company:        Eyematic Interfaces
  3.     Project:        Shout3D Core
  4.     Class:            LabelEffect
  5.     Date:            June 7, 2000
  6.     Description:    An example of a PostRenderEffect that displays a label
  7.     (C) Copyright Eyematic Interfaces, Inc. - 1997/1998/1999/2000 - All rights reserved
  8.  */
  9.  
  10. package custom_nodes;
  11.  
  12. import shout3d.core.*;
  13. import shout3d.*;
  14. import shout3d.core.awt.*;
  15. import java.awt.*;
  16. import java.awt.image.*;
  17.  
  18. /**
  19.  * LabelEffect
  20.  * 
  21.  * @author Jim Stewartson
  22.  */
  23.  
  24. public class LabelEffect extends PostRenderEffect implements FieldObserver{
  25.  
  26.     final public FloatArrayField label_bg_color        = new FloatArrayField(    this, "label_bg_color",                Field.COLOR,    new float[]{0,0,0});
  27.     final public FloatArrayField label_border_color    = new FloatArrayField(    this, "label_border_color",            Field.COLOR,    new float[]{0,1,0});
  28.     final public FloatArrayField label_text_color    = new FloatArrayField(    this, "label_text_color",            Field.COLOR,    new float[]{1,1,0});
  29.     final public StringField     label_text_font    = new StringField(        this, "label_text_font",            Field.ANY,    "Arial");
  30.     final public StringField     label_text            = new StringField(        this, "label_text",                    Field.ANY,    "");
  31.     final public IntField         label_text_size    = new IntField(            this, "label_text_size",            Field.NON_NEGATIVE_INT,    12);
  32.     final public StringField     label_text_style    = new StringField(        this, "label_text_style",            Field.ANY,    "bold");
  33.     final public IntField         xPosition            = new IntField(            this, "xPosition",                    Field.NON_NEGATIVE_INT,    0);
  34.     final public IntField         yPosition            = new IntField(            this, "yPosition",                    Field.NON_NEGATIVE_INT,    0);
  35.  
  36.     static final String PLAIN            = "plain";
  37.     static final String BOLD            = "bold";
  38.     static final String ITALIC            = "italic";
  39.     static final String BOLDITALIC        = "bolditalic";
  40.  
  41.     static final int BG_COLOR        = 0;
  42.     static final int BORDER_COLOR    = 1;
  43.     static final int TEXT_COLOR        = 2;
  44.     static final int TEXT            = 3;
  45.     static final int FONT            = 4;
  46.     static final int SIZE            = 5;
  47.     static final int STYLE            = 6;
  48.     static final int X                = 7;
  49.     static final int Y                = 8;
  50.     
  51.     /**
  52.      * Constructs a default LabelEffect node.
  53.      */
  54.     public LabelEffect(){
  55.         label_bg_color.addFieldObserver(this, new Integer(BG_COLOR));
  56.         label_border_color.addFieldObserver(this, new Integer(BORDER_COLOR));
  57.         label_text_color.addFieldObserver(this, new Integer(TEXT_COLOR));
  58.         label_text.addFieldObserver(this, new Integer(TEXT));
  59.         label_text_font.addFieldObserver(this, new Integer(FONT));
  60.         label_text_size.addFieldObserver(this, new Integer(SIZE));    
  61.         label_text_style.addFieldObserver(this, new Integer(STYLE));    
  62.         xPosition.addFieldObserver(this, new Integer(X));            
  63.         yPosition.addFieldObserver(this, new Integer(Y));                    
  64.     }
  65.  
  66.     public void onFieldChange(Field f, Object userData){
  67.         int whichField = ((Integer)userData).intValue();
  68.         switch(whichField){
  69.             case BG_COLOR:
  70.                 j_label_bg_color = new java.awt.Color(label_bg_color.getValue()[0], label_bg_color.getValue()[1], label_bg_color.getValue()[2]);        
  71.                 break;
  72.             case BORDER_COLOR:
  73.                 j_label_border_color = new java.awt.Color(label_border_color.getValue()[0], label_border_color.getValue()[1], label_border_color.getValue()[2]);        
  74.                 break;
  75.             case TEXT_COLOR:
  76.                 j_label_text_color = new java.awt.Color(label_text_color.getValue()[0], label_text_color.getValue()[1], label_text_color.getValue()[2]);        
  77.                 break;
  78.             case SIZE:        
  79.             case FONT:
  80.             case STYLE:
  81.                 String style = label_text_style.getValue();
  82.                 int javaFontStyle;
  83.                 if (style.equalsIgnoreCase(BOLD)){
  84.                     javaFontStyle = Font.BOLD;
  85.                 }
  86.                 else if (style.equalsIgnoreCase(ITALIC)){
  87.                     javaFontStyle = Font.ITALIC;
  88.                 }
  89.                 else if (style.equalsIgnoreCase(BOLDITALIC)){
  90.                     javaFontStyle = Font.BOLD+Font.ITALIC;
  91.                 }
  92.                 else {
  93.                     javaFontStyle = Font.PLAIN;
  94.                 }
  95.                 font = new Font(label_text_font.getValue(), javaFontStyle, label_text_size.getValue());
  96.                 break;
  97.         }
  98.     }
  99.     
  100.     java.awt.Color j_label_bg_color = new java.awt.Color(0f, 0f, 0f);
  101.     java.awt.Color j_label_border_color = new java.awt.Color(0f, 1f, 0f);
  102.     java.awt.Color j_label_text_color = new java.awt.Color(1f, 1f, 0f);
  103.     
  104.     Font font = new Font("Arial", Font.BOLD, 12);
  105.     public void filter(Graphics g, int surface_pixel_bits[], float z_buffer[], int deviceWidth, int deviceHeight)
  106.     {
  107.         drawLabel(g);
  108.     }
  109.     
  110.     public boolean drawLabel(Graphics g)
  111.     {
  112.         g.setFont(font);
  113.         String string = label_text.getValue();
  114.         if (string != null){
  115.             int x = xPosition.getValue();
  116.             int y = yPosition.getValue();
  117.             int width = g.getFontMetrics().stringWidth(string);
  118.             int height = g.getFontMetrics().getHeight();
  119.             int descent = g.getFontMetrics().getMaxDescent()/2;
  120.             
  121.             g.setColor(j_label_bg_color);
  122.             g.fillRect(x, y, width+4, height+4);
  123.             g.setColor(j_label_border_color);
  124.             g.drawRect(x, y, width+4, height+4);
  125.             g.setColor(j_label_text_color);
  126.             g.drawString(string, x+2, y+height-descent);        
  127.             return true;
  128.         }        
  129.         return false;
  130.     }
  131. }